package cn.lili.modules.order.cart.entity.vo;

import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.modules.distribution.entity.dos.DistributionGoods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.enums.SalesModeEnum;
import cn.lili.modules.order.cart.entity.enums.CartSceneEnum;
import cn.lili.modules.promotion.tools.PromotionTools;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Map;

/**
 * 购物车中的产品
 *
 * @author Chopper
 * @since 2020-03-24 10:33 上午
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class CartSkuVO extends CartBase implements Serializable {


    private static final long serialVersionUID = -894598033321906974L;


    // 预设 编号字段
    private String sn;
    /**
     * 对应的sku DO
     */
    private GoodsSku goodsSku;

    /**
     * 分销描述
     */
    private DistributionGoods distributionGoods;

    @ApiModelProperty(value = "购买数量")
    private Integer num;

    @ApiModelProperty(value = "购买时的成交价")
    private Double purchasePrice;

    @ApiModelProperty(value = "小记")
    private Double subTotal;

    @ApiModelProperty(value = "单价")
    private Double utilPrice;

    @ApiModelProperty(value = "是否选中，选中则下单会进行结算")
    private Boolean checked;

    @ApiModelProperty(value = "是否免运费")
    private Boolean isFreeFreight;

    @ApiModelProperty(value = "是否失效")
    private Boolean invalid;

    @ApiModelProperty(value = "购物车商品错误消息")
    private String errorMessage;

    @ApiModelProperty(value = "是否可配送")
    private Boolean isShip;


    @ApiModelProperty(value = "积分购买 积分数量")
    private Double points;

    @ApiModelProperty(value = "红包购买 红包数量")
    private Integer redPack;

    @ApiModelProperty("商品促销活动集合，key 为 促销活动类型，value 为 促销活动实体信息 ")
    private Map<String, Object> promotionMap;

    @ApiModelProperty(value = "供应商id")
    private String supplierId;

    @ApiModelProperty(value = "供应商名称")
    private String supplierName;

    @ApiModelProperty(value = "当前单品促销类型")
    private PromotionTypeEnum promotionTypeEnum;

    @ApiModelProperty(value = "促销id：" +
            "拼团：则为拼团id为空则为创建拼团" +
            "砍价活动：为砍价活动id" +
            "积分兑换：积分兑换活动id")
    private String promotionId;

    /**
     * @see CartSceneEnum
     */
    @ApiModelProperty(value = "购物车类型")
    private CartSceneEnum cartType;

    /**
     * @see SalesModeEnum
     */
    @ApiModelProperty(value = "商品销售模式", required = true)
    private String salesModel;

    @ApiModelProperty(value = "是否绿色商品")
    private Boolean isGreenGoods = false;

    @ApiModelProperty(value = "绿色积分")
    private Double greenScore;

    @ApiModelProperty(value = "最大F分抵扣值")
    private Double maxfNumber;

    @ApiModelProperty(value = "商品类型", required = true)
    private String goodsType;

    @ApiModelProperty(value = "是否公益商品")
    private Boolean isCommonwealGoods = false;

    @ApiModelProperty(value = "是否绿色积分卷抵扣")
    private Boolean isGreenSecurities;

    @ApiModelProperty(value = "最大绿色积分卷抵扣金额")
    private Double isGreenSecuritiesPrice;

    @ApiModelProperty(value = "是否演唱会门票")
    private Boolean isTicket;

    @ApiModelProperty(value = "是否套餐商品")
    private Boolean isMeal;

    @ApiModelProperty(value = "是否套餐规格商品")
    private Boolean isDefaultMeal;

    @ApiModelProperty(value = "套餐商品价格")
    private Double mealPrice;

    public Double getGreenScore() {
        if(greenScore == null)
            return 0D;
        return greenScore;
    }

    public Double getMaxfNumber() {
        if(maxfNumber == null)
            return 0D;
        return maxfNumber;
    }

    /**
     * 在构造器里初始化促销列表，规格列表
     */
    public CartSkuVO(GoodsSku goodsSku) {
        this.goodsSku = goodsSku;
        if (goodsSku.getUpdateTime() == null) {
            this.goodsSku.setUpdateTime(goodsSku.getCreateTime());
        }
        this.checked = true;
        this.invalid = false;
        //默认时间为0，让系统为此商品更新缓存
        this.errorMessage = "";
        this.isShip = true;
        this.isGreenGoods = goodsSku.getIsGreenGoods();
        this.isCommonwealGoods = goodsSku.getIsCommonwealGoods();
        this.greenScore = goodsSku.getGreenScore();
        this.maxfNumber = goodsSku.getMaxfNumber();
        this.goodsType = goodsSku.getGoodsType();

        log.info("CartSkuVO#mealGoodsSource:{},isMeal:{},isDefaultMeal:{}",
                goodsSku.getMealGoodsSource(), goodsSku.getIsMeal(), goodsSku.getIsDefaultMeal());
        if ((null != goodsSku.getMealGoodsSource() && 0 == goodsSku.getMealGoodsSource())
                && (null != goodsSku.getIsMeal() && goodsSku.getIsMeal())
                && (null != goodsSku.getIsDefaultMeal() && goodsSku.getIsDefaultMeal())) {
            this.purchasePrice = goodsSku.getMealPrice();
        }else {
            this.purchasePrice = goodsSku.getPromotionFlag() != null && goodsSku.getPromotionFlag() ? goodsSku.getPromotionPrice() : goodsSku.getPrice();
        }

        this.isFreeFreight = false;
        this.utilPrice = goodsSku.getPromotionFlag() != null && goodsSku.getPromotionFlag() ? goodsSku.getPromotionPrice() : goodsSku.getPrice();
        this.setStoreId(goodsSku.getStoreId());
        this.setStoreName(goodsSku.getStoreName());
        //如果供应商id为空，则写入-1，表示本店铺商品
        if (goodsSku.getSupplierId() != null) {
            this.supplierId = goodsSku.getSupplierId();
            this.supplierName = goodsSku.getSupplierName();
        } else {
            this.supplierId = "-1";
        }
        this.salesModel = goodsSku.getSalesModel();
        this.isGreenSecurities = goodsSku.getIsGreenSecurities();
        this.isGreenSecuritiesPrice = goodsSku.getIsGreenSecuritiesPrice();
        this.isTicket = goodsSku.getIsTicket();
        this.isMeal = goodsSku.getIsMeal();
        this.mealPrice = goodsSku.getMealPrice();
    }

    /**
     * 在构造器里初始化促销列表，规格列表
     */
    public CartSkuVO(GoodsSku goodsSku, Map<String, Object> promotionMap) {
        this(goodsSku);
        if (promotionMap != null && !promotionMap.isEmpty()) {
            this.promotionMap = promotionMap;
        }
    }

    public Map<String, Object> getPromotionMap() {
        return PromotionTools.filterInvalidPromotionsMap(this.promotionMap);
    }

    public Map<String, Object> notFilterPromotionMap() {
        return this.promotionMap;
    }

    public static void main(String[] args) {

        System.out.println("0 == Integer.valueOf(0) = " + (0 == Integer.valueOf(0)));
    }
}
